package com.team622.shop.common;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;

import java.util.List;

/**
 * 分页结果封装类
 *
 * @param <T> 数据类型
 */
@Data
public class PageResult<T> {
    private Integer status;
    private Integer code;
    private String msg;
    private List<T> list;
    private Long total;
    private Long current;
    private Long pageSize;

    public PageResult() {
    }

    public PageResult(List<T> list, Long total) {
        this.list = list;
        this.total = total;
    }

    public PageResult(List<T> list, Long total, Long current, Long pageSize) {
        this.list = list;
        this.total = total;
        this.current = current;
        this.pageSize = pageSize;
    }

    public static <T> PageResult<T> success(List<T> list, Long total) {
        PageResult<T> result = new PageResult<>(list, total);
        result.setStatus(200);
        result.setCode(20000);
        result.setMsg("操作成功");
        return result;
    }

    public static <T> PageResult<T> success(List<T> list, Long total, Long current, Long pageSize) {
        PageResult<T> result = new PageResult<>(list, total, current, pageSize);
        result.setStatus(200);
        result.setCode(20000);
        result.setMsg("操作成功");
        return result;
    }

    public static <T> PageResult<T> error(String message) {
        PageResult<T> result = new PageResult<>();
        result.setStatus(-1);
        result.setCode(-1);
        result.setMsg(message);
        return result;
    }

    public static <T> PageResult<T> success(IPage<T> page) {
        PageResult<T> result = new PageResult<>(
                page.getRecords(),
                page.getTotal(),
                page.getCurrent(),
                page.getSize()
        );
        result.setStatus(200);
        result.setCode(20000);
        result.setMsg("操作成功");
        return result;
    }
}
